//
//  RegisterViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()

@property (nonatomic, strong) UITextField *phoneNumberTF;//手机号
@property (nonatomic, strong) UITextField *verfiTF;//验证码
@property (nonatomic, strong) UIButton *timerButton; // 倒计时
@property (nonatomic, strong) UIButton *boxButton;//点击同意box
@property (nonatomic, strong) UILabel *agressLabel;//同意协议书
@property (nonatomic, strong) UIButton *loginButton;//登录下一步

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSInteger  secondes;


@end

@implementation RegisterViewController

-(void)viewDidLoad{
    [super viewDidLoad];

    [self setupUI];
}

-(void)viewDidDisappear:(BOOL)animated{

    [self.timer invalidate];
    self.timer = nil;
}
- (void)setupUI{
    
    self.title = @"注册";
    
    //添加控件
    [self.view addSubview:self.phoneNumberTF];
    [self.view addSubview:self.verfiTF];
    [self.view addSubview:self.timerButton];
    [self.view addSubview:self.boxButton];
    [self.view addSubview:self.agressLabel];
    [self.view addSubview:self.loginButton];
    
    
    /**
     *  约束控件
     */
    [self.phoneNumberTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(60 + 40);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.timerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.phoneNumberTF.mas_bottom).offset(30);
        make.right.mas_equalTo(self.phoneNumberTF.mas_right);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(100);
    }];
    
    [self.verfiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timerButton.mas_top);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.timerButton.mas_left).offset(-20);
    }];
    
    //同意协议内容
    [self.boxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.verfiTF.mas_bottom).offset(45);
        make.left.mas_equalTo(self.verfiTF.mas_left).offset(40);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.agressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.boxButton);
        make.left.mas_equalTo(self.boxButton.mas_right).offset(10);
        make.height.equalTo(self.boxButton);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.boxButton.mas_bottom).offset(20);
        make.height.mas_equalTo(45);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
    
    
}

#pragma mark
#pragma mark - 点击同意小点
- (void)clickBoxButton:(UIButton *)box{
    box.selected = !box.isSelected;
    
}

//点击获取验证码
- (void)clickTimerButtion:(UIButton *)btn{

    self.secondes = 10;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerCut) userInfo:nil repeats:YES];
    [self.timer fire];
}

//点击开始注册
- (void)clickRegisterButton:(UIButton *)btn{

    NSLog(@"开始注册");
}

//倒计时开始
- (void)timerCut{

    NSString * stateString = nil;
    if (self.secondes <= 0) {
        self.timerButton.userInteractionEnabled =YES;
        stateString = @"获取验证码";
        [self.timer invalidate];
        self.timer = nil;
    }else{
        self.timerButton.userInteractionEnabled = NO;
        stateString = [NSString stringWithFormat:@"%lds",self.secondes];
    }
    
    self.timerButton.titleLabel.text = @"60s";
    [self.timerButton setTitle:stateString forState:UIControlStateNormal];
    
    self.secondes -= 1;
    
}

#pragma mark
#pragma mark - 懒加载
-(UITextField *)phoneNumberTF{
    if (_phoneNumberTF == nil) {
        _phoneNumberTF = [[UITextField alloc]init];
        _phoneNumberTF.placeholder = @"请输入你的手机号";
        _phoneNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return  _phoneNumberTF;
}

-(UITextField *)verfiTF{

    if (_verfiTF == nil) {
        _verfiTF= [[UITextField alloc]init];
        _verfiTF.placeholder = @"请输入验证码";
        _verfiTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _verfiTF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _verfiTF;
}

- (UIButton *)boxButton{

    if (_boxButton == nil) {
        _boxButton = [[UIButton alloc]init];
        [_boxButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
        [_boxButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
        [_boxButton addTarget:self action:@selector(clickBoxButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _boxButton;
}

//本人同意并阅读,用户协议
-(UILabel *)agressLabel{

    if (_agressLabel == nil) {
        _agressLabel = [[UILabel alloc]init];
        _agressLabel.font = FONT(13);
        
        NSString *agressText = @"本人同意并阅读 用户协议";
        NSMutableAttributedString *attM = [[NSMutableAttributedString alloc]initWithString:agressText];
        
        NSRange range = [agressText rangeOfString:@"用户协议"];
        [attM addAttributes:@{NSFontAttributeName:FONT(14)} range:range];
        _agressLabel.attributedText = attM;
        
        
    }
    return  _agressLabel;
}

-(UIButton *)timerButton{

    if (_timerButton == nil) {
        _timerButton = [[UIButton alloc]init];
        [_timerButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        _timerButton.backgroundColor = [UIColor lightGrayColor];
        _timerButton.layer.cornerRadius = 3;
        _timerButton.layer.masksToBounds = YES;
        _timerButton.titleLabel.font = FONT(16);
        [_timerButton addTarget:self action:@selector(clickTimerButtion:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _timerButton;
}

-(UIButton *)loginButton{

    if (_loginButton == nil) {
        [_loginButton setTitle:@"注册" forState:UIControlStateNormal];
        _loginButton.backgroundColor = kThemeColor;
        _loginButton.titleLabel.font = FONT(20);
        [_loginButton addTarget:self action:@selector(clickRegisterButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _loginButton;
}
@end
